package com.jgp.third.wx.service.impl;

import com.jgp.security.config.SecUserAdapter;
import com.jgp.security.pojo.UserInfo;
import com.jgp.sys.query.OrderList;
import com.jgp.sys.query.QueryFilterList;
import com.jgp.sys.ui.Pager;
import com.jgp.third.wx.model.WxConfig;
import com.jgp.third.wx.model.WxUseFor;
import com.jgp.third.wx.model.WxUserInfo;
import com.jgp.third.wx.repository.WxConfigRepository;
import com.jgp.third.wx.repository.WxUseForRepository;
import com.jgp.third.wx.repository.WxUserInfoRepository;
import com.jgp.third.wx.service.WxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/6/2
 */
@Service
public class WxServiceImpl implements WxService {

    @Autowired
    private SecUserAdapter userAdapter;

    @Autowired
    private WxConfigRepository configRepository;

    @Autowired
    private WxUserInfoRepository userInfoRepository;

    @Autowired
    private WxUseForRepository useForRepository;

    @Transactional(readOnly = true)
    @Override
    public WxConfig queryConfigByForId(Long useForId) {
        WxUseFor useFor = queryUseForById(Long.valueOf(useForId));
        return configRepository.read(useFor.getConfigId());
    }

    @Transactional
    @Override
    public void saveWxUser(WxUserInfo userInfo) {
        String appId = userInfo.getAppId();
        String openId = userInfo.getOpenId();
        WxUserInfo user = queryUserByOpenIdAndAppId(openId,appId);
        if(Objects.nonNull(user)){
            userInfo.setId(user.getId());
            userInfoRepository.update(userInfo);
        }else{
            userInfoRepository.create(userInfo);
        }
    }
    @Transactional(readOnly = true)
    @Override
    public WxUserInfo queryUserByOpenIdAndAppId(String openId, String appId) {
        QueryFilterList filters = new QueryFilterList();
        filters.addFilter("appId","eq",appId);
        filters.addFilter("openId","eq",openId);
        //只可能有一个
        List<WxUserInfo> users = userInfoRepository.read(filters);
        if(users.size()>0){
            return users.get(0);
        }else{
            return null;
        }
    }
    @Transactional(readOnly = true)
    @Override
    public WxUseFor queryUseForById(Long useForId) {
        return useForRepository.read(useForId);
    }
    @Transactional(readOnly = true)
    @Override
    public List<WxUseFor> queryUseFor(String name, Pager pager) {
        UserInfo user = userAdapter.currentUser();
        if (Objects.nonNull(user)) {
            QueryFilterList filters = new QueryFilterList();
            filters.addFilter("adminUserId", "eq", user.getId());
            if (StringUtils.isNotBlank(name)) {
                filters.addFilter("name", "like", name);
            }
            OrderList orders = new OrderList();
            orders.addOrder("createDatetime","desc");
            return  useForRepository.read(filters,orders, pager);
        }
        return null;
    }
    @Transactional
    @Override
    public WxUseFor saveUseFor(WxUseFor useFor) {
        if(Objects.nonNull(useFor.getId())){
            useForRepository.update(useFor);
        }else {
            UserInfo user = userAdapter.currentUser();
            useFor.setAdminUserId(user.getId());
            useFor = useForRepository.create(useFor);
        }
        return useFor;
    }
    @Transactional(readOnly = true)
    @Override
    public List<WxConfig> queryConfig(String name, Pager pager) {
        UserInfo user = userAdapter.currentUser();
        if (Objects.nonNull(user)) {
            QueryFilterList filters = new QueryFilterList();
            filters.addFilter("adminUserId", "eq", user.getId());
            if (StringUtils.isNotBlank(name)) {
                filters.addFilter("name", "like", name);
            }
            OrderList orders = new OrderList();
            orders.addOrder("createDatetime","desc");
            return configRepository.read(filters, orders,pager);
        }

        return null;
    }
    @Transactional
    @Override
    public WxConfig saveConfig(WxConfig config) {
        UserInfo user = userAdapter.currentUser();
        config.setAdminUserId(user.getId());
        return configRepository.createOrUpdate(config);
    }
    @Transactional
    @Override
    public void removeConfig(Long id) {
        configRepository.delete(id);
    }
    @Transactional
    @Override
    public void removeUseFor(Long useForId) {
        useForRepository.delete(useForId);
    }

    @Override
    public void bindConfigToUseFor(Long useForId, Long configId) {
        WxUseFor useFor = useForRepository.read(useForId);
        useFor.setConfigId(configId);
        useForRepository.update(useFor,"configId");
    }


}
